﻿using System;
using System.Windows.Forms;
using ExtractItAll.Extractors;

namespace ExtractItAll.Forms
{
  public partial class MainForm : Form
  {
    private bool _Started;

    public MainForm()
    {
      InitializeComponent();
      InitInfo();
    }

    private void InitInfo()
    {
      notifyIcon.Text = String.Format("{0} v{1}",
                                      Program.VersionInfo.ProductName,
                                      Program.VersionInfo.FileVersion);


      infoButton.DropDownItems.Clear();
      infoButton.DropDownItems.Add(String.Format("{0} v{1}",
                                                 Program.VersionInfo.ProductName,
                                                 Program.VersionInfo.FileVersion)).Enabled = false;
      infoButton.DropDownItems.Add("Build: " + Program.BuildTime.ToUniversalTime()).Enabled = false;

      infoButton.DropDownItems.Add(new ToolStripSeparator());

      foreach (BaseExtractor e in Program.ExtractorList)
      {
        infoButton.DropDownItems.Add(e.InfoText).Enabled = false;
      }

      infoButton.DropDownItems.Add(new ToolStripSeparator());

      ToolStripMenuItem updateCheckToolStripMenuItem = new ToolStripMenuItem("Check for Updates");
      updateCheckToolStripMenuItem.Click += UpdateCheck_Click;
      //updateCheckToolStripMenuItem.Image = global::ExtractItAll.Properties.Resources.globe;
      infoButton.DropDownItems.Add(updateCheckToolStripMenuItem);

      ToolStripMenuItem openWebsiteToolStripMenuItem = new ToolStripMenuItem("Project Website");
      openWebsiteToolStripMenuItem.Click += OpenWebsite_Click;
      openWebsiteToolStripMenuItem.Image = global::ExtractItAll.Properties.Resources.globe;
      infoButton.DropDownItems.Add(openWebsiteToolStripMenuItem);
    }

    private void OpenWebsite_Click(object sender, EventArgs e)
    {
      System.Diagnostics.Process.Start(Program.ProjectWebsite);
    }

    private void UpdateCheck_Click(object sender, EventArgs e)
    {
      Program.Settings.LastUpdateCheck = DateTime.Now;
      UpdateCheck.Check(Program.UpdateFile);
    }


    private void toolStripSplitButton1_Click(object sender, EventArgs e)
    {
      if (!_Started)
      {
        Program.Passwords.Reset();
        Program.Tasks.Start();
        _Started = true;
        startStopButton.Text = "Stop";
        startStopButton.Image = global::ExtractItAll.Properties.Resources.Stop;
      }
      else
      {
        Program.Tasks.Stop();
        _Started = false;
        startStopButton.Text = "Start";
        startStopButton.Image = global::ExtractItAll.Properties.Resources.Start;
      }
    }

    private void Form1_Resize(object sender, EventArgs e)
    {
      if (WindowState == FormWindowState.Minimized
          && Program.Settings.MinimizeToTray)
      {
        //this.notifyIcon.ContextMenu = contextMenu1;
        WindowState = FormWindowState.Minimized;
        ShowInTaskbar = false;
        notifyIcon.Visible = true;
      }
    }

    private void notifyIcon_DoubleClick(object sender, EventArgs e)
    {
      notifyIcon.Visible = false;
      ShowInTaskbar = true;
      WindowState = FormWindowState.Normal;
    }
  }
}